package bb.lanxing.util.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import bb.lanxing.databinding.LayoutDropdownChooseItemBinding

class DropDownItemAdapter(
    private val items: List<DropDownItem>, private val listener: (DropDownItem) -> Unit
) : RecyclerView.Adapter<DropDownItemAdapter.ViewHolder>() {

    class ViewHolder(
        private val adapter: DropDownItemAdapter,
        private val binding: LayoutDropdownChooseItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: DropDownItem) {
            binding.title.text = item.text
            binding.image.setImageDrawable(
                ContextCompat.getDrawable(
                    binding.root.context, item.icon
                )
            )
            itemView.setOnClickListener {
                adapter.listener(item)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate = LayoutDropdownChooseItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(this, inflate)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }
}